// pages/ct/ct.js
Page({

  data: {
    ksList: [],
    questions: [], // 题目数据
    currentIndex: 0, // 当前题目索引
    userAnswers: [],
    submitted: false, // 是否已交卷
    results: { // 交卷后的结果
      correct: 0,
      incorrect: 0
    },
    options: [],
    lst: []
  },


  onLoad(options) {
    let wrongQuestionIds = wx.getStorageSync('wrongQuestionIds') || [];
    this.setData({
      ksList: wrongQuestionIds
    })
    console.log(this.data.ksList)
    const ksList=this.data.ksList
    const totalItems = ksList.length;
    const sjksList = []
    const numToSelect = ksList.length+1;
    if (totalItems <= numToSelect) {
      sjksList.push(...ksList);
    } else {
      for (let i = 0; i < numToSelect; i++) {
        sjksList.push(ksList[i]);
      }
    }
    this.setData({
      questions: sjksList
    })
    console.log(this.data.questions)
    const extractedData = this.data.questions.map(question => {
      const extracted = {
        id: question.tmxh,
        value: question.tmzqda,
        tmdas: []
      };
      for (let i = 1; i <= parseInt(question.tmdas, 10); i++) {
        const key = `tmda${i}`;
        if (question[key]) {
          extracted.tmdas.push({
            tmda: question[key]
          });
        }
      }
      return extracted;
    });
    this.setData({
      options: extractedData
    });
    console.log(this.data.options)
  },
  // 上一题
  prevQuestion() {
    if (this.data.currentIndex > 0) {
      this.setData({
        currentIndex: this.data.currentIndex - 1
      });
    }
  },

  // 下一题
  nextQuestion() {
    if (this.data.currentIndex < this.data.questions.length - 1) {
      this.setData({
        currentIndex: this.data.currentIndex + 1
      });
    }
  },

  // 交卷
  submit() {
    let correct = 0;
    let incorrect = 0;
    const userAnswers = this.data.userAnswers;
    const questions = this.data.questions;
    let sum=questions.length;
    const lst = [];
    const wrongQuestionIds = []; // 用于临时存储答错的题目ID
    for (let i = 0; i < questions.length; i++) {
      const question = questions[i].tmzqda;
      const optionIndexes = [];
      for (let j = 0; j < question.length; j++) {
        if (question[j] === '1') {
          optionIndexes.push(j);
        }
      }
      lst.push(optionIndexes);

    }
    console.log(userAnswers)
    console.log(lst)

    for (let i = 0; i < userAnswers.length; i++) {
      const questionall = questions[i];
      const userAnswer = userAnswers[i];
      const correctAnswer = lst[i][0];
      const a = lst[i]; // 直接访问单个元素的数组中的元素

      if (Array.isArray(userAnswer)) {
        // 严格比较：用户答案数组必须只包含正确答案中的单个元素
        const convertedArray1 = userAnswer.map(Number);
        if (convertedArray1.length !== a.length) {
          incorrect++;
          wrongQuestionIds.push(questionall)
        } else {
          // 排序数组
          const sortedArr1 = [...convertedArray1].sort((a, b) => a - b);
          const sortedArr2 = [...a].sort((a, b) => a - b);
          if (JSON.stringify(sortedArr1) === JSON.stringify(sortedArr2)) {
            correct++;
          } else {
            incorrect++;
            wrongQuestionIds.push(questionall)
          }
        }
      } else if (typeof userAnswer === 'string') {
        // 用户答案是字符串，直接比较
        if (userAnswer === correctAnswer.toString()) {
          correct++;
        } else {
          incorrect++;
          wrongQuestionIds.push(questionall)
        }
      } else {
        // 类型不匹配，错误计数增加
        incorrect++;
        wrongQuestionIds.push(questionall)
      }
    }
    this.setData({
      correct: correct,
      incorrect: incorrect,
    })
    console.log(correct)
    console.log(incorrect)
    console.log(wrongQuestionIds)
    wx.setStorageSync('wrongQuestionIds', wrongQuestionIds);
    wx.showModal({
      title: '提示',
      content: '是否结束答题',
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/tjym/tjym?correct=' + correct + "&incorrect=" + incorrect + "&sum=" + sum,
          })
          console.log('用户点击确定');
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },
  checkboxChange: function (e) {
    console.log(e)
    const index = e.currentTarget.dataset.index;
    const value = e.detail.value;

    this.setData({
      ['userAnswers[' + index + ']']: value
    });
  }
})